// packageF/sachertorte/purchase/purchase.js
let app = getApp();
let location = require("../../../mybehaviors/location/location");
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    purchaseTime: 30 * 60 * 60 * 1000,
    timeData:{},
    page:1,
    isLoadMore: true,
    totalPage: 0,
    city:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.city) {
      this.setData({
        city: options.city,
      })
    }
    this.recommendList();
  },
  goodsNav(event) {
    let goodsId = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/packageF/sachertorte/sachertorteGoods/sachertorteGoods?id=${goodsId}&city=${this.data.city}`,
    })
  },
  onTimeChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  recommendList() {
    let query = {
      page:this.data.page,
      city: this.data.city
    }
    let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          let goods = res.data.data;
          let date = new Date(goods[0]?.promotion_end_at.replace(/-/g,'/'));
          let timeDate = date.getTime();
          let current = new Date().getTime();
          this.setData({
            goods: res.data.data,
            purchaseTime: timeDate - current
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
        city: this.data.city
      }
      let urlStr = app.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods");
      app._getNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              goods : this.data.goods.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    try {
      this._getLocation((mapdata, e) => { //获取定位的城市，拿到定位的信息
        let city = mapdata.address_component.city;
        if(!this.data.city) {
          this.setData({
            city:city
          });
        }
        //定位后调用首页接口和推荐列表接口
        this.recommendList();
      });
    } catch (e) {
      console.log(e);
      this.recommendList();
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      isLoadMore: true,
      page: 1,
      totalPage: 0
    });
    this.getMoreData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})